#include "functions.h"
#define GAME_CONTINUE 1
#define GAME_RESTART -1
#define GAME_EXIT -2
void color(short x) //改变颜色
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数，改变字体颜色
}

void show_unity()
{
    gotoxy(0, 0);
    printf("\n"
           "               &#########@ \n"
           "             $##@%%:.'&####%% \n"
           "         &#        !@#$'|##                                 @$    ;#@:\n"
           "      &##        !##|  '&#$       !#@:   !#@!   #  !&##!    ^^   ######   $#$    !#@\n"
           "    @##$`       |##!    !##       !#@:   !#@!   %%#$'`|##    #@'    #@:    :@#   :@#\n"
           ":;@###############@:      |       !#@:   !#@!   %%#%%  |#$    #@'    #@:     ;##;.%%#`\n"
           ":.%%###&|^^^^^^^^$##|     '&       !#@:   !#@!   %%#%%  |#$    #@'    #@:      |#&$# \n"
           "    |###!       ;##     |##       :@#&;:%%##@!   %%#%%  |#$    #@'    ##:      `$## \n"
           "      |###!      '&#   :@#%%        `$####  $;   !&|  !@|    @$'    %%&|:     ;##\n"
           "        !###|.     $#  %%#@:                                               .|@#\n"
           "             |####  |&####|\n"
           "                !&####### '\n"
           "                      ##|:\n");
}

void animation()
{
    system("cls");
    color(8); //第一帧
    show_unity();
    delay(600);

    color(7);
    show_unity(); //第二帧
    delay(700);

    color(15); //第三帧
    show_unity();
    delay(1000);
}

bool win_ani(bool flag, char map[][WIDTH], int board_pos1[], int board_pos2[], struct ball *ball1, struct ball *ball2, struct ball *ball3)
{
    extern int who_win,ctrl_ball2,ctrl_ball3;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); //变黄
    if (who_win == LEFTWIN)
    {
        printf("Press 'r' to restart (space to exit)");
        int ctrl=GAME_CONTINUE;
        while (ctrl)
        {
            gotoxy(0, 1);
            printf(
                " _     _____ _____ _____  __        _____ _   _ _\n"
                "| |   | ____|  ___|_   _| \\ \\      / /_ _| \\ | | |\n"
                "| |   |  _| | |_    | |    \\ \\ /\\ / / | ||  \\| | |\n"
                "| |___| |___|  _|   | |     \\ V  V /  | || |\\  |_|\n"
                "|_____|_____|_|     |_|      \\_/\\_/  |___|_| \\_(_)\n");

            ctrl = delay_ani(950 , ctrl); //在此更改闪烁间隔，单位毫秒
            if(ctrl<0) break;
            
            gotoxy(0, 1);
            printf(
                "                                                 \n"
                "                                                     \n"
                "                                                      \n"
                "                                                    \n"
                "                                                     \n");
            ctrl = delay_ani(700 , ctrl); //在此更改闪烁间隔，单位毫秒
            if(ctrl<0) break;
        }

        if (ctrl == GAME_EXIT)
        {
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
            return false;
        }
        else if (ctrl == GAME_RESTART)
        {
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);

            init_map(map);
            board_pos1[X_POS] = 3;
            board_pos1[Y_POS] = HEIGHT/2;
            board_pos2[X_POS] = WIDTH - 3; //初始化，暂时先这样
            board_pos2[Y_POS] = HEIGHT/2;
            (ball1->pos)[X_POS] = 1;
            (ball1->pos)[Y_POS] = 2;
            (ball1->dirc)[X_POS] = 1;
            (ball1->dirc)[Y_POS] = 1;
            (ball1->speed) = BALL_SPEED; //主球
        
            (ball2->pos)[X_POS] = 1;
            (ball2->pos)[Y_POS] = 1;
            (ball2->dirc)[X_POS] = 1;
            (ball2->dirc)[Y_POS] = 1;
            (ball2->speed) = PROP_SPEED_P1;    //道具球
        
            (ball3->pos)[X_POS] = 30;
            (ball3->pos)[Y_POS] = 1;
            (ball3->dirc)[X_POS] = -1;
            (ball3->dirc)[Y_POS] = 1;
            (ball3->speed) = PROP_SPEED_P2;    //道具球
        
            board(map, board_pos1, board_pos2);
            printout(map);
            who_win = -1;
            ctrl_ball2 = 0;
            ctrl_ball3 = 0;

            return true;
        }
        
    }
    
    else if (who_win == RIGHTWIN)
    {
        printf("Press 'r' to restart (space to exit)");
        int ctrl=GAME_CONTINUE;
        while (ctrl)
        {
            gotoxy(0, 1);
            printf(
                " ____  ___ ____ _   _ _____  __        _____ _   _ _\n"
                "|  _ \\|_ _/ ___| | | |_   _| \\ \\      / /_ _| \\ | | |\n"
                "| |_) || | |  _| |_| | | |    \\ \\ /\\ / / | ||  \\| | |\n"
                "|  _ < | | |_| |  _  | | |     \\ V  V /  | || |\\  |_|\n"
                "|_| \\_\\___\\____|_| |_| |_|      \\_/\\_/  |___|_| \\_(_)\n");

            ctrl = delay_ani(950,ctrl); //在此更改闪烁间隔，单位毫秒
            if(ctrl<0) break;

            gotoxy(0, 1);

            printf(
                "                                                    \n"
                "                                                     \n"
                "                                                      \n"
                "                                                     \n"
                "                                                     \n");

            ctrl = delay_ani(700,ctrl); //在此更改闪烁间隔，单位毫秒
            if(ctrl<0) break;

        }
        
        if (ctrl == GAME_EXIT)
        {
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
            return false;
        }
        else if (ctrl == GAME_RESTART)
        {
            system("cls");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);

            init_map(map);
            board_pos1[X_POS] = 3;
            board_pos1[Y_POS] = HEIGHT/2;
            board_pos2[X_POS] = WIDTH - 3; //初始化，暂时先这样
            board_pos2[Y_POS] = HEIGHT/2;
            (ball1->pos)[X_POS] = 1;
            (ball1->pos)[Y_POS] = 2;
            (ball1->dirc)[X_POS] = 1;
            (ball1->dirc)[Y_POS] = 1;
            (ball1->speed) = BALL_SPEED; //主球
        
            (ball2->pos)[X_POS] = 1;
            (ball2->pos)[Y_POS] = 1;
            (ball2->dirc)[X_POS] = 1;
            (ball2->dirc)[Y_POS] = 1;
            (ball2->speed) = PROP_SPEED_P1;    //道具球
        
            (ball3->pos)[X_POS] = 30;
            (ball3->pos)[Y_POS] = 1;
            (ball3->dirc)[X_POS] = -1;
            (ball3->dirc)[Y_POS] = 1;
            (ball3->speed) = PROP_SPEED_P2;    //道具球
        
            board(map, board_pos1, board_pos2);
            printout(map);
            who_win = -1;
            ctrl_ball2 = 0;
            ctrl_ball3 = 0;
            
            return true;
        }
        
    }
}



int delay_ani(int milisec , int ctrl)
{
    int begain = clock();
    while (clock() < begain + milisec)
        if(GetKeyState(0x52)<0)
            return GAME_RESTART;
        else if(GetKeyState(0x20)<0) 
            return GAME_EXIT;   
    
    return 1;        
}